//
//  EMMemberCenterViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/21.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMMemberCenterViewController.h"
#import "UINavigationController+EMNavi.h"
#import "EMLoginViewController.h"
#import "EMMyMessageViewController.h"
#import "EMAppraisalViewController.h"
#import "EMMemberInfoViewController.h"
#import "EMFeedbackViewController.h"
#import "EMAboutViewController.h"
#import "EMMyOrderViewController.h"
#import "EMSettingViewController.h"
#import "SYSMViewController.h"
#import "SHFWViewController.h"
#import "UserInfo.h"


#import "EMGetAddressReqBody.h"
#import "EMGetAddressRespBody.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
//
#import "EMAddressMgrViewController.h"

@interface EMMemberCenterViewController ()
{
    UITableView *moreTable;
    BOOL moreTableVisible;
    AppDelegate *app;
    UITableView *memberCenterTable;
    UILabel *userNameLabel;
}
@end

@implementation EMMemberCenterViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"个人中心";
    moreTableVisible = NO;
    NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
    [memberCenterTable reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 60, 32);
    button.titleLabel.font = [UIFont fontWithName:HEITILIGHT size:16];
    button.layer.borderColor = RGB(50, 144, 4).CGColor;
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 5;
    [button setTitle:@"更多" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem =item;
}

- (void)listRemove
{
    for (UIView *table in self.view.subviews) {
        if (table.tag == 1001) {
            [table removeFromSuperview];
        }
    }
    moreTableVisible = NO;
}
- (void)moreAction:(UIButton *)sender
{
    moreTable = [[UITableView alloc] initWithFrame:CGRectMake(W(150), 64, 160, 180) style:UITableViewStylePlain];
    moreTable.delegate = self;
    moreTable.dataSource = self;
    moreTable.layer.borderColor = [UIColor grayColor].CGColor;
    moreTable.layer.borderWidth = 0.5;
    moreTable.layer.cornerRadius = 5;
    moreTable.scrollEnabled = NO;
    moreTable.tag = 1001;
    if (moreTableVisible == YES) {
        [self listRemove];
        
    } else {
        [self.view addSubview:moreTable];
        moreTableVisible = YES;
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(W(50), H(90), W(220), H(30))];

    memberCenterTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    memberCenterTable.delegate = self;
    memberCenterTable.dataSource = self;
    memberCenterTable.backgroundColor = RGB(242, 242, 242);
    memberCenterTable.bounces = NO;
    memberCenterTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:memberCenterTable];
    
    self.view.backgroundColor = RGB(242, 242, 242);
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag == 1001) {
        return 1;
    } else {
        return 3;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 1001) {
        return 4;
    } else {
        switch (section) {
            case 0:
                return 1;
                break;
            case 1:
                return 4;
                break;
            case 2:
                return 1;
                break;
                
            default:
                return 0;
                break;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 1001) {
        static NSString *cellIdentifier = @"moreCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont fontWithName:HEITIMEDIUM size:14];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
        UIImageView *icon = [[UIImageView alloc] init];
        icon.frame = CGRectMake(5, 0, 45, 45);
        [cell addSubview:icon];
        switch (indexPath.row) {
                
            case 0:
            {
                icon.image = [UIImage imageNamed:@"icon_个人中心_使用帮助"];
                cell.textLabel.text = @"使用帮助";
                break;
            }
            case 1:
            {
                icon.image = [UIImage imageNamed:@"icon_个人中心_意见反馈"];
                cell.textLabel.text = @"意见反馈";
                break;
            }
            case 2:
            {
                icon.image = [UIImage imageNamed:@"icon_个人中心_检测更新"];
                cell.textLabel.text = @"检测更新";
                break;
            }
            case 3:
            {
                icon.image = [UIImage imageNamed:@"icon_个人中心_关于沃佳"];
                cell.textLabel.text = @"关于沃佳";
                break;
            }
            default:
                break;
        }

        return cell;
    } else {
        static NSString *cellIdentifier = @"memCenterCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.section == 0) {
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"背景_个人中心_用户"]];
            UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_用户"]];
            imgView.frame = CGRectMake(W(132.5), H(25), W(55), W(55));
            [cell addSubview:imgView];
            userNameLabel.textAlignment = NSTextAlignmentCenter;
            userNameLabel.font = [UIFont fontWithName:HEITIMEDIUM size:14];
            [cell addSubview:userNameLabel];
            
            if ([UserInfo sharedUserInfo].m_isLogin) {
                userNameLabel.text = [NSString stringWithFormat:@"欢迎您，%@", [UserInfo sharedUserInfo].m_nickName];
            } else {
                userNameLabel.text = @"点击登陆";
            }
            
        } else if (indexPath.section == 1) {
            
            UIImageView *icon = [[UIImageView alloc] init];
            icon.frame = CGRectMake(W(15), H(5), W(38), H(38));
            [cell addSubview:icon];
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(W(56), H(10), W(65), H(25))];
            titleLabel.textColor = [UIColor grayColor];
            titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:titleLabel];
            UIImageView *go = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_前进"]];
            go.frame = CGRectMake(W(277), H(5), W(30), H(35));
            [cell addSubview:go];

            if (indexPath.row != 0) {
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.5)];
                lineView.backgroundColor = [UIColor lightGrayColor];
                [cell addSubview:lineView];
            }
            
            
            switch (indexPath.row) {
                case 0:
                {
                    icon.image = [UIImage imageNamed:@"icon_个人中心_全部订单"];
                    titleLabel.text = @"我的订单";
                    break;
                }
                case 1:
                {
                    icon.image = [UIImage imageNamed:@"icon_个人中心_我的消息"];
                    titleLabel.text = @"我的消息";
                    break;
                }
                case 2:
                {
                    icon.image = [UIImage imageNamed:@"icon_个人中心_商品评价"];
                    titleLabel.text = @"商品评价";
                    break;
                }
                case 3:
                {
                    icon.image = [UIImage imageNamed:@"icon_个人中心_个人信息"];
                    titleLabel.text = @"个人信息";
                    break;
                }
                default:
                    break;
            }
        } else {
            UIButton *postSlaeButton = [UIButton buttonWithType:UIButtonTypeCustom];
            postSlaeButton.frame = CGRectMake(0, 0, W(cell.frame.size.width / 3), H(120));
            postSlaeButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
            postSlaeButton.layer.borderWidth = 1;
            
            UIImageView *postIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_售后服务"]];
            postIcon.frame = CGRectMake(W(30), postSlaeButton.frame.size.height - H(105), W(50), H(50));
            [postSlaeButton addSubview:postIcon];
            UILabel *postSaleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, postSlaeButton.frame.size.height - H(60), postSlaeButton.frame.size.width, H(30))];
            postSaleLabel.text = @"售后服务";
            postSaleLabel.textColor = [UIColor grayColor];
            postSaleLabel.textAlignment = NSTextAlignmentCenter;
            postSaleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [postSlaeButton addTarget:self action:@selector(postSaleAction:) forControlEvents:UIControlEventTouchUpInside];
            [postSlaeButton addSubview:postSaleLabel];
            [cell addSubview:postSlaeButton];

        
            UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
            exitButton.frame = CGRectMake(W(cell.frame.size.width / 3 - 1), 0, W(cell.frame.size.width / 3 + 2), H(120));
            exitButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
            exitButton.layer.borderWidth = 1;
            UIImageView *exitIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_安全退出"]];
            exitIcon.frame = CGRectMake(W(30), postSlaeButton.frame.size.height - H(105), W(50), H(50));
            [exitButton addSubview:exitIcon];
            UILabel *exitLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, exitButton.frame.size.height - H(60), exitButton.frame.size.width, H(30))];
            exitLabel.text = @"安全退出";
            exitLabel.textColor = [UIColor grayColor];
            exitLabel.textAlignment = NSTextAlignmentCenter;
            exitLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [exitButton addTarget:self action:@selector(exitAction:) forControlEvents:UIControlEventTouchUpInside];
            [exitButton addSubview:exitLabel];
            [cell addSubview:exitButton];
            
            
            UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
            settingButton.frame = CGRectMake(W(cell.frame.size.width * 2 / 3), 0, W(cell.frame.size.width / 3), H(120));
            settingButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
            settingButton.layer.borderWidth = 1;
            UIImageView *settingIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_设置"]];
            settingIcon.frame = CGRectMake(W(30), postSlaeButton.frame.size.height - H(105), W(50), H(50));
            [settingButton addSubview:settingIcon];
            UILabel *settingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, settingButton.frame.size.height - H(60), settingButton.frame.size.width, H(30))];
            settingLabel.text = @"设置";
            settingLabel.textColor = [UIColor grayColor];
            settingLabel.textAlignment = NSTextAlignmentCenter;
            settingLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [settingButton addTarget:self action:@selector(settingAction:) forControlEvents:UIControlEventTouchUpInside];
            [settingButton addSubview:settingLabel];
            [cell addSubview:settingButton];

            
            

        }
        return cell;

    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1001) {
        return 45;
    } else {
        switch (indexPath.section) {
            case 0:
                return H(140);
                break;
            case 1:
                return H(45);
                break;
            case 2:
                return H(120);
                break;
                
            default:
                return 0;
                break;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView.tag == 1001) {
        return 0;
    } else {
        return H(5);
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 5)];
    footerView.backgroundColor = RGB(242, 242, 242);
    return footerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self listRemove];
    if (tableView.tag == 1001) {
        switch (indexPath.row) {
            case 0:
            {
                SYSMViewController *controller = [[SYSMViewController alloc] init];
                controller.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:controller animated:YES];
                break;
            }
            case 1:
            {
                if ([UserInfo sharedUserInfo].m_isLogin) {
                    EMFeedbackViewController *controller = [[EMFeedbackViewController alloc] init];
                    controller.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:controller animated:YES];
                } else {
                    EMLoginViewController *controller = [[EMLoginViewController alloc] init];
                    controller.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:controller animated:YES];
                }
                
                break;
            }
            case 2:
            {
                [self showAlert:@"［检测更新］正在开发中。。。"];
                break;
            }
            case 3:
            {
                EMAboutViewController *controller = [[EMAboutViewController alloc] init];
                controller.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:controller animated:YES];
                break;
            }
            default:
                break;
        }

    } else {
        if (indexPath.section == 0) {
            if (![UserInfo sharedUserInfo].m_isLogin) {
                EMLoginViewController *controller = [[EMLoginViewController alloc] init];
                controller.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:controller animated:YES];
                
            }
        } else if (indexPath.section == 1) {
            switch (indexPath.row) {
                case 0:
                {
                    if ([UserInfo sharedUserInfo].m_isLogin) {
                        EMMyOrderViewController *controller = [[EMMyOrderViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    } else {
                        [app showTip:@"请先登录！"];
                        EMLoginViewController *controller = [[EMLoginViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    }
                    
                    break;
                }
                case 1:
                {
                    if ([UserInfo sharedUserInfo].m_isLogin) {
                        EMMyMessageViewController *controller = [[EMMyMessageViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    } else {
                        [app showTip:@"请先登录！"];
                        EMLoginViewController *controller = [[EMLoginViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    }
                    
                    break;
                }
                case 2:
                {
                    if ([UserInfo sharedUserInfo].m_isLogin) {
                        EMAppraisalViewController *controller = [[EMAppraisalViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    } else {
                        [app showTip:@"请先登录！"];
                        EMLoginViewController *controller = [[EMLoginViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    }
                    
                    break;
                }
                case 3:
                {
                    if ([UserInfo sharedUserInfo].m_isLogin) {
                        EMMemberInfoViewController *controller = [[EMMemberInfoViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    } else {
                        [app showTip:@"请先登录！"];
                        EMLoginViewController *controller = [[EMLoginViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:controller animated:YES];
                    }
                    
                    break;
                }
                default:
                    break;
            }
        }
    }

}
- (void)postSaleAction:(UIButton *)sender
{
    SHFWViewController *controller = [[SHFWViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)exitAction:(UIButton *)sender
{
    if ([UserInfo sharedUserInfo].m_isLogin) {
        EMAttention *attention = [[EMAttention alloc] initWithTitle:@"提示" contentText:@"您确定要退出吗" leftButtonTitle:@"取消" rightButtonTitle:@"确定"];
        [attention setRightBlock:^() {
            [UserInfo sharedUserInfo].m_isLogin = NO;
            [UserInfo sharedUserInfo].m_session = @"";
            [UserInfo sharedUserInfo].m_userName = @"";
            [UserInfo sharedUserInfo].m_nickName = @"";
            NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
            [memberCenterTable reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
        [attention show];
    } else {
        [app showTip:@"您都没有登录哎，怎么能退出呢！"];
    }
   
}
- (void)settingAction:(UIButton *)sender
{
    EMSettingViewController *controller = [[EMSettingViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)showAlert:(NSString *)content
{
    EMAttention *attention = [[EMAttention alloc] initWithTitle:@"提示" contentText:content leftButtonTitle:nil rightButtonTitle:@"确定"];
    [attention show];
}
@end
